package config

import (
	"github.com/samuel/go-zookeeper/zk"
	"github.com/spf13/viper"
	"go.etcd.io/etcd/client/v3"
	"log"
)

var (
	Etcd        EtcdConf
	MysqlConfig MysqlConf
	TraceConfig TraceConf
	Zk          ZookeeperConf
)

type ZookeeperConf struct {
	ZkConn        *zk.Conn
	SecProductKey string //商品键
}

type EtcdConf struct {
	EtcdConn          *clientv3.Client //链接
	EtcdSecProductKey string           //商品键
	Host              string
}

type TraceConf struct {
	Host string
	Port string
	Url  string
}

type MysqlConf struct {
	Host  string
	Port  string
	User  string
	Pwd   string
	Db    string
	Table string
}

func InitConfig() {
	viper.AutomaticEnv()
	viper.SetConfigName("config")
	viper.AddConfigPath("./")
	viper.SetConfigType("yaml")
}

func SubParse(key string, value interface{}) error {

	log.Println("Config pre:", key)
	sub := viper.Sub(key)
	return sub.Unmarshal(value)
}
